package com.study.easyexcel.business.comment;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.CommentData;
import com.alibaba.excel.metadata.data.RichTextStringData;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.math.BigDecimal;

/**
 * 给字段所在的单元格添加备注
 *
 * @author yuhuan
 * @date 2023/5/14
 */
public class AssetsComment implements Converter<BigDecimal> {

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public BigDecimal convertToJavaData(ReadConverterContext<?> context) throws Exception {
        return context.getReadCellData().getNumberValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<BigDecimal> context) throws Exception {
        WriteCellData<Object> writeCellData = new WriteCellData<>(context.getValue());
        CommentData commentData = new CommentData();
        writeCellData.setCommentData(commentData);
        commentData.setAuthor("study api");
        commentData.setRichTextStringData(new RichTextStringData("请替换成您的备注信息"));
        // 备注的默认大小是按照单元格的大小
        // 这里想调整到4个单元格那么大 所以向后 向下 各额外占用了一个单元格
        commentData.setRelativeLastColumnIndex(1);
        commentData.setRelativeLastRowIndex(1);
        return writeCellData;
    }
}
